package com.gz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gz.entity.Book;
import com.gz.service.BookService;
import com.gz.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 龙龙与贝儿
* @description 针对表【t_book】的数据库操作Service实现
* @createDate 2025-05-12 17:03:02
*/
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
    implements BookService{

    @Autowired
    BookMapper bookMapper;

    @Override
    public Page<Book> getBookAll(int pageNum, int pageSize, String keyword) {
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        if (keyword != null && keyword.length() > 0) {
            wrapper.and(e->e.like(Book::getName,keyword)
                    .or().like(Book::getAuthor,keyword)
                    .or().like(Book::getDetail,keyword)
                    .or().like(Book::getPublisher,keyword));
        }
        wrapper.select(Book::getId,Book::getName,Book::getAuthor,Book::getImgSrc,Book::getPrice,Book::getBuyCount);
        Page<Book> page = new Page<>(pageNum,pageSize);
        bookMapper.selectPage(page,wrapper);
        return page;
    }

    @Override
    @Cacheable(cacheNames = "books",keyGenerator = "keyGenerator")
    public Page<Book> getBookTypeById(Integer typeId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        /*因为热销榜的id是0，且没有与只想对应的图书；但是按照常理来将，热销榜本来就不应该有对应的图书
         * 而应该是动态的，谁多谁就上来
         * */
        if (typeId == 0){
            wrapper.orderByDesc(Book::getBuyCount);

        }else {
            wrapper.eq(Book::getTypeId,typeId);

        }
        wrapper.select(Book::getId,Book::getName,Book::getAuthor,Book::getImgSrc,Book::getPrice,Book::getBuyCount);
        Page<Book> page = new Page<>();
        if (typeId == 0){
            page = new Page<>(1,6);

        }else {
            page = new Page<>(pageNum,pageSize);

        }
        bookMapper.selectPage(page,wrapper);
        return page;
    }

    @Override
    public Book getBookById(Long bookId) {
        Book book = bookMapper.selectById(bookId);
        return book;
    }

    @Override
    public Page<Book> getBookByKeyword(String searchText, int pageNum, int pageSize) {
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.and(e->e.like(Book::getName,searchText)
                .or().like(Book::getAuthor,searchText)
                .or().like(Book::getDetail,searchText)
                .or().like(Book::getPublisher,searchText));
        wrapper.select(Book::getId,Book::getName,Book::getAuthor,Book::getImgSrc,Book::getPrice,Book::getBuyCount);
        Page<Book> page = new Page<>(pageNum,pageSize);
        bookMapper.selectPage(page,wrapper);
        return page;
    }
}




